﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LJLib.PBCom;
using LJLib.Tools.File;
using SanrossDll;

namespace SDK_Demo
{
    public partial class Form1 : Form
    {
        private static P1ClientCom proxy; //通信模块


        #region 门店信息

        private static string sptcode = "801-2000-006"; //供应商编号

        public static string taskcode1 = "DSCG20190504"; //电商采购订单号1

        #endregion


        private static readonly string xmlFile = "config.xml";
        private static XmlConfig xmlConfig = new XmlConfig();

        public Form1()
        {
            InitializeComponent();
            var arg_msg = string.Empty;
            proxy = new P1ClientCom();
            if (!proxy.Init("127.0.0.1", 30020, ref arg_msg))
            {
                MessageBox.Show(arg_msg);
            }

            tbFactoryToken.Text = xmlConfig.GetXmlFileValue(xmlFile, string.Empty, "factoryToken", string.Empty);
            tbShopToken.Text = xmlConfig.GetXmlFileValue(xmlFile, string.Empty, "shopToken", string.Empty);

            tbFactoryUsercode.Text = xmlConfig.GetXmlFileValue(xmlFile, string.Empty, "factoryUsercode", string.Empty);
            tbShopUsercode.Text = xmlConfig.GetXmlFileValue(xmlFile, string.Empty, "shopUsercode", string.Empty);

            tbUsercode.Text = xmlConfig.GetXmlFileValue(xmlFile, string.Empty, "usercode", string.Empty);
            tbPassword.Text = xmlConfig.GetXmlFileValue(xmlFile, string.Empty, "password", string.Empty);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            tbShopUsercode.Text = tbShopUsercode.Text.Trim();

            if (string.IsNullOrEmpty(tbShopUsercode.Text))
            {
                tbShopUsercode.Focus();
                tbShopUsercode.SelectAll();
                MessageBox.Show("请录入账号名");
                return;
            }
            //门店注册
            //var shopToken = "{" + Guid.NewGuid() + "}";
            //var cusid = "{" + Guid.NewGuid() + "}";

            // RegCompany,构建请求参数
            var request = proxy.NewPBDictionary();
            request.SetString("comname", "杭州华邦房地产"); // 门店全称
            request.SetString("simplename", "华邦地产"); // 门店简称
            request.SetString("LegalPerson", "问计划"); // 企业法人
            request.SetString("license", "1231sda"); // 营业执照注册编号
            request.SetString("RegAddress", "广东梅州"); // 注册地址
            request.SetString("business", "中国"); // 经营范围
            request.SetString("custype", "阿俊"); // business
            request.SetString("tele", "12312390909"); // 门店联系电话
            request.SetString("address", "浙江安吉"); // 门店联系地址
            request.SetString("freight", "货运部"); // 门店货运部
            request.SetString("freight_tele", "12345"); // 门店货运部电话
            //request.SetString("token", shopToken); // GUID门店管理员身份标识，用于之后身份标识
            request.SetString("dscrp", ""); // 门店简介
            request.SetString("usercode", tbShopUsercode.Text); // 门店管理员账号
            request.SetString("psw", tbShopUsercode.Text); // 门店管理员密码
            //request.SetString("cusid", cusid);// // GUID企业的唯一ID

            var respone = proxy.DoExecute("RegCompany", request);
            var errmsg = respone.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }

            //xmlConfig.SetXmlFileValue(xmlFile, string.Empty, "shopToken", shopToken);
            xmlConfig.SetXmlFileValue(xmlFile, string.Empty, "shopUsercode", tbShopUsercode.Text);
            MessageBox.Show("注册成功！");
  
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //BindCopy
            var request = proxy.NewPBDictionary();
            request.Clear();
            request.SetString("token", tbShopToken.Text);
            request.SetString("usercode", tbUsercode.Text);
            request.SetString("psw", tbPassword.Text);
            request.SetInt("bindtype", 0);
            request.SetString("relcode", sptcode); // 供应商编号
            var respone = proxy.DoExecute("BindCopy", request);
            var errmsg = respone.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }
            MessageBox.Show("绑定成功!");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // UploadSKU
            var request = proxy.NewPBDictionary();
            request.Clear();
            request.SetString("token", tbShopToken.Text);
            var mtrlList = proxy.NewPBArray();
            var item1 = proxy.NewPBDictionary();
            item1.SetInt("skuflag", 1);
            item1.SetString("mtrlcode", "1070005");
            item1.SetString("status", "");
            item1.SetString("woodcode", "");
            item1.SetString("pcode", "");
            //item1.SetInt("mtrlid", 11);
            item1.SetString("mtrlname", "A产品");
            item1.SetString("mtrlmode", "");
            item1.SetString("unit", "套");

            mtrlList.AddObject(item1);

            var item2 = proxy.NewPBDictionary();
            item2.SetInt("skuflag", 1);
            item2.SetString("mtrlcode", "1070002");
            item2.SetString("status", "");
            item2.SetString("woodcode", "");
            item2.SetString("pcode", "");
            //item2.SetInt("mtrlid", 12);
            item2.SetString("mtrlname", "B产品");
            item2.SetString("mtrlmode", "");
            item2.SetString("unit", "套");

            mtrlList.AddObject(item2);

            request.SetObject("mtrlList", mtrlList);

            var respone = proxy.DoExecute("UploadSKU", request);
            var errmsg = respone.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }
            MessageBox.Show("上传SKU成功");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //UploadLocalBuyTasks
            var request = proxy.NewPBDictionary();
            request.Clear();
            request.SetString("token", tbShopToken.Text);
            var buytaskList = proxy.NewPBArray();
            var item1 = proxy.NewPBDictionary();
            item1.SetString("buytaskcode", taskcode1); // 采购订单号
            item1.SetString("sptcode", sptcode); //供应商编码
            item1.SetDateTime("requiredate", DateTime.Now);
            item1.SetString("opemp", "阿俊");
            item1.SetDateTime("opdate", DateTime.Now);
            item1.SetString("relcode", "1023001");  // 相关号非必填

            var skuList1 = proxy.NewPBArray();
            var sku1 = proxy.NewPBDictionary();
            sku1.SetInt("skuflag", 1);
            sku1.SetString("mtrlcode", "wl181024001");
            sku1.SetString("status", "");
            sku1.SetString("woodcode", "");
            sku1.SetString("pcode", "");
            sku1.SetString("mtrlname", "A产品");
            sku1.SetString("mtrlmode", "");
            sku1.SetDouble("qty", 1);
            sku1.SetString("dscrp", "");
            sku1.SetString("dscrp2", "");
            sku1.SetDouble("price", 250);
            sku1.SetString("unit", "套");

            skuList1.AddObject(sku1);

            var sku2 = proxy.NewPBDictionary();
            sku2.SetInt("skuflag", 1);
            sku2.SetString("mtrlcode", "wl181024002");
            sku2.SetString("status", "");
            sku2.SetString("woodcode", "");
            sku2.SetString("pcode", "");
            sku2.SetString("mtrlname", "B产品");
            sku2.SetString("mtrlmode", "");
            sku2.SetDouble("qty", 2);
            sku2.SetString("dscrp", "");
            sku2.SetString("dscrp2", "");
            sku2.SetDouble("price", 300);
            sku2.SetString("unit", "张");

            skuList1.AddObject(sku2);

            item1.SetObject("skus", skuList1);

            buytaskList.AddObject(item1);

            request.SetObject("buytasks", buytaskList);

            var respone = proxy.DoExecute("UploadLocalBuyTasks", request);
            var errmsg = respone.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }
            MessageBox.Show("上传订单成功");

        }

        private void button6_Click(object sender, EventArgs e)
        {
            // GetLocalBuyTasksInfo
            var request = proxy.NewPBDictionary();
            request.Clear();
            request.SetString("token", tbShopToken.Text);
            var taskcodes = proxy.NewPBArray();
            taskcodes.AddString(taskcode1);
            //taskcodes.AddString("002");
            request.SetObject("taskcodes", taskcodes);

            var respone = proxy.DoExecute("GetLocalBuyTasksInfo", request);
            var errmsg = respone.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }

            richTextBox1.Text = string.Format("GetLocalBuyTasksInfo返回：{0}", respone) + "\r\n" + richTextBox1.Text;

            MessageBox.Show("获取订单成功！");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            var request = proxy.NewPBDictionary();
            request.Clear();
            request.SetString("token", tbShopToken.Text);
            var outwares = proxy.NewPBArray();
            
            var outware1 = proxy.NewPBDictionary();
            outware1.SetString("taskcode", taskcode1);
            outware1.SetString("outwarecode", "BDFH2018103100001");
            outware1.SetString("carcode", "粤X-99999");
            outware1.SetString("transcode", "SF100001");
            outware1.SetString("dscrp", "顺丰快递");

            var skuList1 = proxy.NewPBArray();
            var sku1 = proxy.NewPBDictionary();
            sku1.SetInt("skuflag", 1);
            sku1.SetString("mtrlcode", "wl181024001");
            sku1.SetString("status", "");
            sku1.SetString("woodcode", "");
            sku1.SetString("pcode", "");
            sku1.SetString("mtrlname", "A产品");
            sku1.SetString("mtrlmode", "");
            sku1.SetString("unit", "套");
            sku1.SetDouble("qty", 1);

            var fxbarcodes1 = proxy.NewPBArray();
            //条码1
            var barcode1 = proxy.NewPBDictionary();
            barcode1.SetString("barcode", "18110200006");
            barcode1.SetDouble("qty", 1);
            barcode1.SetDouble("packqty", 1);
            fxbarcodes1.AddObject(barcode1);

            sku1.SetObject("barcodes", fxbarcodes1);
            skuList1.AddObject(sku1);

            var sku2 = proxy.NewPBDictionary();
            sku2.SetInt("skuflag", 1);
            sku2.SetString("mtrlcode", "wl181024002");
            sku2.SetString("status", "");
            sku2.SetString("woodcode", "");
            sku2.SetString("pcode", "");
            sku2.SetString("mtrlname", "B产品");
            sku2.SetString("mtrlmode", "");
            sku2.SetString("unit", "张");
            sku2.SetDouble("qty", 2);

            var fxbarcodes2 = proxy.NewPBArray();
            //条码2
            var barcode2 = proxy.NewPBDictionary();
            barcode2.SetString("barcode", "18110200007");
            barcode2.SetDouble("qty", 1);
            barcode2.SetDouble("packqty", 1);
            fxbarcodes2.AddObject(barcode2);

            //条码3
            var barcode3 = proxy.NewPBDictionary();
            barcode3.SetString("barcode", "18110200008");
            barcode3.SetDouble("qty", 1);
            barcode3.SetDouble("packqty", 1);

            fxbarcodes2.AddObject(barcode3);

            sku2.SetObject("barcodes", fxbarcodes2);
            skuList1.AddObject(sku2);

            outware1.SetObject("skus", skuList1);

            outwares.AddObject(outware1);

            request.SetObject("outwares", outwares);

            var respone = proxy.DoExecute("UploadLocalOutwares", request);
            var errmsg = respone.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }

            richTextBox1.Text = string.Format("UploadLocalOutwares返回：{0}", respone) + "\r\n" + richTextBox1.Text;

            MessageBox.Show("上传发货条码成功！");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            // 工厂注册

            tbFactoryUsercode.Text = tbFactoryUsercode.Text.Trim();

            if (string.IsNullOrEmpty(tbFactoryUsercode.Text))
            {
                tbFactoryUsercode.Focus();
                tbFactoryUsercode.SelectAll();
                MessageBox.Show("请录入账号名");
                return;
            }

            var factoryToken = "{" + Guid.NewGuid() + "}";
            var cusid = "{" + Guid.NewGuid() + "}";

            // RegCompany,构建请求参数
            var request = proxy.NewPBDictionary();
            request.SetString("comname", "测试工厂全称");
            request.SetString("simplename", "测试工厂简称");
            request.SetString("custype", "供应商"); // 工厂联系人
            request.SetString("tele", "12312390909"); // 工厂联系人电话
            request.SetString("address", "广州"); // 工厂联系地址
            request.SetString("freight", "货运部"); // 工厂货运部
            request.SetString("freight_tele", "12345"); // 工厂货运部电话
            request.SetString("token", factoryToken); // GUID工厂管理员身份标识，用于之后身份标识
            request.SetString("dscrp", ""); // 简介
            request.SetString("usercode", tbFactoryUsercode.Text); // 管理员账号
            request.SetString("psw", tbFactoryUsercode.Text); // 管理员密码
            request.SetString("cusid", cusid);// GUID企业的唯一ID

            var respone = proxy.DoExecute("RegCompany", request);
            var errmsg = respone.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }

            tbFactoryToken.Text = factoryToken;
            xmlConfig.SetXmlFileValue(xmlFile, string.Empty, "factoryToken", factoryToken);
            xmlConfig.SetXmlFileValue(xmlFile, string.Empty, "factoryUsercode", tbFactoryUsercode.Text);
            MessageBox.Show("注册成功！");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            // 工厂提交客户资料及分配客户管理员账号

            tbUsercode.Text = tbUsercode.Text.Trim();
            if (string.IsNullOrEmpty(tbUsercode.Text))
            {
                tbUsercode.Focus();
                tbUsercode.SelectAll();
                MessageBox.Show("请输入用户名");
                return;
            }

            int comid = 0;
            string cuscode = "C001";

            var req = proxy.NewPBDictionary();
            req.SetString("token", tbFactoryToken.Text);
            var rsp = proxy.DoExecute("GetPartnerCompanys", req);
            var errmsg = rsp.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }

            var rsltList = rsp.GetPBArray("Companys");

            for (int i = 0; i < rsltList.Count; i++)
            {
                var item = rsltList.GetPBDictionary(i);
                var relationtype = item.GetInt("relationtype");
                var relcode = item.GetString("relcode");
                if (relationtype == 0 && relcode == cuscode)
                {
                    comid = item.GetInt("comid");
                    break;
                }
            }

            // 没有C001客户就创建一个
            if (comid == 0)
            {
                var com = proxy.NewPBDictionary();
                com.SetInt("comid", 0); // 新建客户
                com.SetInt("inuse", 1); // 有效
                com.SetInt("relationtype", 0); // 0-客户 1-供应商
                com.SetString("relcode", cuscode); // 客户编码
                com.SetInt("relid", 11); // 客户ID
                com.SetString("comname", "客户001"); // 客户名称
                com.SetString("simplename", "客户001"); // 客户简称
                com.SetString("precode", string.Empty); // 条码前缀
                com.SetString("custype", "联系人"); // 客户联系人
                com.SetString("tele", "123456789"); // 客户联系人电话
                com.SetString("address", ""); // 客户联系人地址
                com.SetString("freight", "");
                com.SetString("freight_tele", "");
                com.SetString("erpsccode", "");
                com.SetString("relareastr", "");
                com.SetString("erpOutrepName", "");
                com.SetString("erpOutrepTele", "");
                com.SetString("erpOutrepTele1", "");
                com.SetString("fx_title_erp", "");
                com.SetInt("interval", 30);
                com.SetInt("taskinterval", 30);

                var list = proxy.NewPBArray();
                list.AddObject(com);

                var request = proxy.NewPBDictionary();
                request.SetString("token", tbFactoryToken.Text);
                request.SetObject("companies", list);

                var response = proxy.DoExecute("AddCompany", request);
                errmsg = response.GetString("ErrMsg");
                if (!string.IsNullOrEmpty(errmsg))
                {
                    MessageBox.Show(errmsg);
                    return;
                }

                rsp = proxy.DoExecute("GetPartnerCompanys", req);
                errmsg = rsp.GetString("ErrMsg");
                if (!string.IsNullOrEmpty(errmsg))
                {
                    MessageBox.Show(errmsg);
                    return;
                }

                rsltList = rsp.GetPBArray("Companys");

                for (int i = 0; i < rsltList.Count; i++)
                {
                    var item = rsltList.GetPBDictionary(i);
                    var relationtype = item.GetInt("relationtype");
                    var relcode = item.GetString("relcode");
                    if (relationtype == 0 && relcode == cuscode)
                    {
                        comid = item.GetInt("comid");
                        break;
                    }
                }
                if (comid == 0)
                {
                    MessageBox.Show("创建C001客户失败");
                    return;
                }
            }

            // 添加用户
            var userinfo = proxy.NewPBDictionary();
            userinfo.SetInt("userid", 0);
            userinfo.SetInt("inuse", 1);
            userinfo.SetInt("usertype", 0);
            userinfo.SetString("usercode", tbUsercode.Text); // 用户名
            userinfo.SetString("username", tbUsercode.Text); // 真实名
            userinfo.SetString("psw", tbPassword.Text); // 密码
            userinfo.SetString("tele", string.Empty);
            userinfo.SetString("email", string.Empty);
            userinfo.SetString("qq", string.Empty);
            userinfo.SetString("address", string.Empty);


            req.SetInt("relationtype", 0); // 客户
            req.SetString("relcode", cuscode); // 客户编号
            req.SetObject("userinfo", userinfo); // 新用户信息

            rsp = proxy.DoExecute("AddUser", req);
            errmsg = rsp.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }
            xmlConfig.SetXmlFileValue(xmlFile, string.Empty, "usercode", tbUsercode.Text);
            xmlConfig.SetXmlFileValue(xmlFile, string.Empty, "password", tbPassword.Text);
            MessageBox.Show("创建客户与管理用户成功");
        }

        private void button10_Click(object sender, EventArgs e)
        {
            // 工厂获取待配对企业
            var req = proxy.NewPBDictionary();
            req.SetString("token", tbFactoryToken.Text);

            var rsp = proxy.DoExecute("GetUnmatchCompanyList", req);
            var errmsg = rsp.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }

            var msg = string.Format("GetUnmatchCompanyList返回：{0}", rsp);
            richTextBox1.Text = msg + "\r\n" + richTextBox1.Text;

            var rsltList = rsp.GetPBArray("companyList");

            for (int i = 0; i < rsltList.Count; i++)
            {
                var item = rsltList.GetPBDictionary(i);

                var comid = item.GetInt("comid");

                var request = proxy.NewPBDictionary();
                request.SetString("token", tbFactoryToken.Text);
                request.SetInt("cuscomid", comid);
                var response = proxy.DoExecute("GetUnmatchSKUs", request);
                richTextBox1.Text = string.Format("GetUnmatchSKUs返回：{0}", response) + "\r\n" + richTextBox1.Text;

                // 上传配对SKU

                var skus = response.GetPBArray("skus");

                var rqskus = proxy.NewPBArray();

                for (int j = 0; j < skus.Count; j++)
                {
                    var sku = skus.GetPBDictionary(j);
                    sku.SetString("fc_mtrlcode", string.Format("MTRL{0}", j));
                    sku.SetString("fc_status", string.Empty);
                    sku.SetString("fc_woodcode", string.Empty);
                    sku.SetString("fc_pcode", string.Empty);

                    rqskus.AddObject(sku);
                }

                request.Clear();
                request.SetString("token", tbFactoryToken.Text);
                request.SetObject("matchSkus", rqskus);
                richTextBox1.Text = string.Format("配对：{0}", request) + "\r\n" + richTextBox1.Text;

                response = proxy.DoExecute("UploadMatchSKUs", request);
                richTextBox1.Text = string.Format("UploadMatchSKUs返回：{0}", response) + "\r\n" + richTextBox1.Text;
                errmsg = response.GetString("ErrMsg");
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            // 工厂上传配对SKU
            
        }

        private void button12_Click(object sender, EventArgs e)
        {
            // 工厂接收订单

            var request = proxy.NewPBDictionary();
            request.SetString("token", tbFactoryToken.Text);
            request.SetInt("clientMode", 1); // 工厂ERP
            request.SetInt("status", 0); // 未下单
            request.SetInt("dateRangeMode", 0); // 按下单日期条件
            request.SetDateTime("begindate", DateTime.Today.AddDays(-100));
            request.SetDateTime("enddate", DateTime.Today.AddDays(100));
            request.SetInt("queryMode", 0);
            request.SetString("querystr", "");
            request.SetInt("pageindex", 1);
            request.SetInt("pagesize", 100);

            var response = proxy.DoExecute("GetTaskList", request);
            richTextBox1.Text = string.Format("GetTaskList返回：{0}", response) + "\r\n" + richTextBox1.Text;
            var errmsg = response.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }

            var tids = new HashSet<string>();

            var tasklist = response.GetPBArray("tasklist");

            for (int i = 0; i < tasklist.Count; i++)
            {
                var item = tasklist.GetPBDictionary(i);

                var tid = item.GetString("tid");

                if (!tids.Contains(tid))
                {
                    tids.Add(tid);
                }
            }


            // 工厂接收门店的采购订单
            foreach (var tid in tids)
            {
                request.Clear();
                request.SetString("token", tbFactoryToken.Text);
                request.SetString("tid", tid);
                request.SetString("opemp", "张三"); // 工厂接单人
                request.SetString("taskcode", "ERP_" + tid);
                var rsp = proxy.DoExecute("AcceptFxBuytask", request);
                richTextBox1.Text = string.Format("接收订单:{0}, 返回:{1}", request, rsp) + "\r\n" + richTextBox1.Text;

                request.Clear();
                request.SetString("token", tbFactoryToken.Text);
                request.SetString("tid", tid);
                request.SetString("opemp", "张四"); // 工厂报价确认人
                rsp = proxy.DoExecute("ConfirmBuytaskPriceEdit", request);
                richTextBox1.Text = string.Format("报价确认:{0}, 返回:{1}", request, rsp) + "\r\n" + richTextBox1.Text;

            }
        }

        
        private void button3_Click(object sender, EventArgs e)
        {
            if (_tids == null)
            {
                MessageBox.Show("请先查询在进行订单");
                return;
            }

            // 工厂上传订单排产状态 UpdateSaletaskFstatus
            foreach (var tid in _tids)
            {
                var request = proxy.NewPBDictionary();
                request.SetString("token", tbFactoryToken.Text);
                request.SetString("tid", tid);
                request.SetInt("updatetype", 0); // 0-更新排产状态 1-更新进仓状态
                request.SetInt("fsstatus", 200); // 0:未排产 100:已排产 200:已进仓 300:已发货
                var response = proxy.DoExecute("UpdateSaletaskFstatus", request);
                richTextBox1.Text = string.Format("UpdateSaletaskFstatus返回：{0}", response) + "\r\n" + richTextBox1.Text;
            }
        }

        private void button11_Click_1(object sender, EventArgs e)
        {
            if (_taskmxList == null)
            {
                MessageBox.Show("请先查询在进行订单");
                return;
            }

            // 工厂上传订单工组进度 EditWrkgrpJd
            for (int i = 0; i < _taskmxList.Count; i++)
            {
                var taskmx = _taskmxList.GetPBDictionary(i);

                var request = proxy.NewPBDictionary();
                request.SetString("token", tbFactoryToken.Text);
                request.SetInt("taskid", taskmx.GetInt("taskid"));
                request.SetInt("printid", taskmx.GetInt("printid"));
                request.SetString("opemp", "张五"); // 工组进度上传人

                var jds = proxy.NewPBArray();

                for (int j = 0; j < 3; j++)
                {
                    var jd = proxy.NewPBDictionary();
                    jd.SetInt("taskid", taskmx.GetInt("taskid"));
                    jd.SetInt("printid", taskmx.GetInt("printid"));
                    jd.SetInt("jdprintid", j + 1);
                    jd.SetString("wrkgrpname", string.Format("工组{0}", j + 1));
                    jd.SetDouble("orderqty", 100);
                    jd.SetDouble("taskqty", 90);
                    jd.SetDouble("finishqty", 80);

                    jds.AddObject(jd);
                }

                request.SetObject("list", jds);

                var response = proxy.DoExecute("EditWrkgrpJd", request);
                richTextBox1.Text = string.Format("EditWrkgrpJd返回：{0}", response) + "\r\n" + richTextBox1.Text;

            }

        }

        private HashSet<string> _tids = null;
        private IPBArray _taskmxList = null;
        private void button15_Click(object sender, EventArgs e)
        {
            // 工厂查询在进行订单
            var request = proxy.NewPBDictionary();
            request.SetString("token", tbFactoryToken.Text);
            request.SetInt("clientMode", 1); // 工厂ERP
            request.SetInt("status", 4); // 0-未下单 2-已退回 3-已下单 4-待付款 5-待付款确认 6-进行中 7-完成 8-待收货（发货中） 9-待拆单 10-拆单待审 11-待报价

            request.SetInt("dateRangeMode", 0); // 按下单日期条件
            request.SetDateTime("begindate", DateTime.Today.AddDays(-100));
            request.SetDateTime("enddate", DateTime.Today.AddDays(100));
            request.SetInt("queryMode", 0);
            request.SetString("querystr", "");
            request.SetInt("pageindex", 1);
            request.SetInt("pagesize", 100);

            var response = proxy.DoExecute("GetTaskList", request);
            richTextBox1.Text = string.Format("GetTaskList返回：{0}", response) + "\r\n" + richTextBox1.Text;
            var errmsg = response.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }

            _tids = new HashSet<string>();

            var tasklist = response.GetPBArray("tasklist");
            _taskmxList = tasklist;

            for (int i = 0; i < tasklist.Count; i++)
            {
                var item = tasklist.GetPBDictionary(i);

                var tid = item.GetString("tid");

                if (!_tids.Contains(tid))
                {
                    _tids.Add(tid);
                }
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (_tids == null)
            {
                MessageBox.Show("请先查询在进行订单");
                return;
            }

            // 工厂上传订单进仓状态 UpdateSaletaskFstatus
            foreach (var tid in _tids)
            {
                var request = proxy.NewPBDictionary();
                request.SetString("token", tbFactoryToken.Text);
                request.SetString("tid", tid);
                request.SetInt("updatetype", 1); // 0-更新排产状态 1-更新进仓状态
                request.SetInt("fsstatus", 200); // 0:未排产 100:已排产 200:已进仓 300:已发货

                var dict = new Dictionary<string, IPBDictionary>();

                var skus = proxy.NewPBArray();

                for (int i = 0; i < _taskmxList.Count; i++)
                {
                    var mx = _taskmxList.GetPBDictionary(i);

                    var mtrlcode = mx.GetString("mtrlcode");
                    var status = mx.GetString("status");
                    var woodcode = mx.GetString("woodcode");
                    var pcode = mx.GetString("pcode");

                    var key = string.Format("mtrlcode:{0},status:{1},woodcode:{2},pcode:{3}", mtrlcode, status, woodcode, pcode);

                    if (dict.ContainsKey(key))
                    {
                        continue;
                    }

                    var item = proxy.NewPBDictionary();
                    item.SetString("mtrlcode", mtrlcode);
                    item.SetString("status", status);
                    item.SetString("woodcode", woodcode);
                    item.SetString("pcode", pcode);
                    item.SetString("storagename", "成品仓");

                    skus.AddObject(item);
                }

                request.SetObject("skus", skus);

                var response = proxy.DoExecute("UpdateSaletaskFstatus", request);
                richTextBox1.Text = string.Format("UpdateSaletaskFstatus返回：{0}", response) + "\r\n" + richTextBox1.Text;
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (_taskmxList == null)
            {
                MessageBox.Show("请先查询在进行订单");
                return;
            }

            // 工厂上传订单分配数 EditSaletaskmxQtyReady

            var request = proxy.NewPBDictionary();
            request.SetString("token", tbFactoryToken.Text);

            var list = proxy.NewPBArray();

            for (int i = 0; i < _taskmxList.Count; i++)
            {
                var taskmx = _taskmxList.GetPBDictionary(i);

                var item = proxy.NewPBDictionary();
                item.SetInt("taskid", taskmx.GetInt("taskid"));
                item.SetInt("printid", taskmx.GetInt("printid"));
                item.SetString("mtrlname", taskmx.GetString("mtrlname"));
                item.SetDouble("qty_ready", taskmx.GetDouble("qty"));

                list.AddObject(item);
            }

            request.SetObject("list", list);

            var response = proxy.DoExecute("EditSaletaskmxQtyReady", request);
            richTextBox1.Text = string.Format("EditSaletaskmxQtyReady返回：{0}", response) + "\r\n" + richTextBox1.Text;

        }

        private void button16_Click(object sender, EventArgs e)
        {

        }

        private void button18_Click(object sender, EventArgs e)
        {
            //确认收货数
            var request = proxy.NewPBDictionary();
            request.SetString("token", tbFactoryToken.Text);

            var list = proxy.NewPBArray();

            IPBArray _outWareList = null;
            for (int i = 0; i < _outWareList.Count; i++)
            {
                var item = proxy.NewPBDictionary();
                item.SetInt("outwareid", 0);
                item.SetInt("logid", 0);
                item.SetString("taskid", string.Empty);
                item.SetInt("printid", 0);
                item.SetDouble("acceptQty", 0);
                item.SetString("acceptUserName", string.Empty);

                list.AddObject(item);
            }

            request.SetObject("FxOutwareLogs", list);

            var response = proxy.DoExecute("UpdateFxoutwareAcceptQty", request);
            var errmsg = response.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }
            MessageBox.Show("确认成功！");
        }

        private void button19_Click(object sender, EventArgs e)
        {
            //GetBindingApplyList
            var request = proxy.NewPBDictionary();
            request.Clear();
            request.SetString("token", tbShopToken.Text);

            var respone = proxy.DoExecute("GetBindingApplyList", request);
            var errmsg = respone.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }

            richTextBox1.Text = string.Empty;
            richTextBox1.Text = string.Format("GetBindingApplyList返回：{0}", respone) + "\r\n" + richTextBox1.Text;

            MessageBox.Show("获取待绑定企业列表成功！");
        }

        private void button20_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("请输入要绑定的主键id");
                return;
            }
            var bindingId = Convert.ToInt32(textBox1.Text);
            //ReplyBindingApply
            var request = proxy.NewPBDictionary();
            request.Clear();
            request.SetString("token", tbShopToken.Text);
            request.SetInt("bindingId", bindingId);
            request.SetString("sptcode", sptcode);
            request.SetInt("flag", 1); //1-同意 2-拒绝
            request.SetString("dscrp", string.Empty);//回复附言
            var respone = proxy.DoExecute("ReplyBindingApply", request);
            var errmsg = respone.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }
            MessageBox.Show("同意绑定成功!");
        }

        private void button21_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("请输入要绑定的主键id");
                return;
            }
            var bindingId = Convert.ToInt32(textBox1.Text);
            //ReplyBindingApply
            var request = proxy.NewPBDictionary();
            request.Clear();
            request.SetString("token", tbShopToken.Text);
            request.SetInt("bindingId", bindingId);
            request.SetInt("flag", 2); //1-同意 2-拒绝
            request.SetString("dscrp", string.Empty);//回复附言
            var respone = proxy.DoExecute("ReplyBindingApply", request);
            var errmsg = respone.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }
            MessageBox.Show("拒绝绑定成功!");
        }

        private void button22_Click(object sender, EventArgs e)
        {
            //Login
            var request = proxy.NewPBDictionary();
            request.Clear();
            request.SetString("usercode", tbShopUsercode.Text);
            request.SetString("psw", tbShopUsercode.Text);
            var respone = proxy.DoExecute("Login", request);
            var errmsg = respone.GetString("ErrMsg");
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }
            var shopToken = respone.GetString("token");
            tbShopToken.Text = shopToken;
            xmlConfig.SetXmlFileValue(xmlFile, string.Empty, "shopToken", shopToken);
            MessageBox.Show("登录成功!");
        }

        private void button23_Click(object sender, EventArgs e)
        {
            //TODO:获取库存示例
        }

        private void button24_Click(object sender, EventArgs e)
        {
            //TODO:获取发货单示例
        }
    }
}
